﻿using System;
using System.Globalization;
using ContractManagementSystem.BusinessLogicLayer;
using ContractManagementSystem.EntityObjects;

namespace ContractManagementSystem.Presentation.ProfileManagement
{
    public partial class ViewConsultant : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckUserPermission();
            if (Session["RoleId"] != null && Session["UserName"] != null)
            {
                DisableTextBox();
                if (!IsPostBack)
                    LoadDataConsultant();
            }
            else
            {
                Response.Redirect("../LogOn.aspx");
            }
        }
        // when load this page info client not edit 
        protected void DisableTextBox()
        {
            textConsultantId.Enabled = false;
            textUserName.Enabled = false;
            textFullName.Enabled = false;
            textCompanyName.Enabled = false;
            textAddress.Enabled = false;
            textPhoneNumber.Enabled = false;
            textEmail.Enabled = false;
        }
        //when user click button edit enable textbox allow edit info
        protected void EnableTextBox()
        {
            textConsultantId.Enabled = false;
            textUserName.Enabled = false;
            textFullName.Enabled = false;
            textCompanyName.Enabled = true;
            textCompanyName.CssClass = "CPFInputField cpfdetailText";
            textAddress.Enabled = true;
            textAddress.CssClass = "CPFInputField cpfdetailText";
            textPhoneNumber.Enabled = true;
            textPhoneNumber.CssClass = "CPFInputField cpfdetailText";
            textEmail.Enabled = false;
        }
        //load info client by role
        protected void LoadDataConsultant()
        {
            var roleId = int.Parse(Session["RoleId"].ToString(), CultureInfo.InvariantCulture);
            if (roleId == 1)
            {
                ButtonEdit.Visible = false;
                ButtonSave.Visible = false;
                LoadUpdateConsultant();
            }
            else if (roleId == 4)
            {
                LoadConsultantInfo();
            }
        }
        private void LoadUpdateConsultant()
        {
            var id = Request.QueryString["id"];
            if (!string.IsNullOrEmpty(id))
            {
                var dataTable = ConsultantProfileLogic.SelectConsultantByConsultantId(id);
                textConsultantId.Text = id;
                textUserName.Text = dataTable.Rows[0]["UserName"].ToString();
                textFullName.Text = dataTable.Rows[0]["FullName"].ToString();
                textCompanyName.Text = dataTable.Rows[0]["CompanyName"].ToString();
                textAddress.Text = dataTable.Rows[0]["Address"].ToString();
                textPhoneNumber.Text = dataTable.Rows[0]["PhoneNumber"].ToString();
                textEmail.Text = dataTable.Rows[0]["Email"].ToString();
            }
        }
        private void LoadConsultantInfo()
        {
            var userName = Session["UserName"].ToString();
            var dataTable = ConsultantProfileLogic.SelectConsultantByUserName(userName);
            textConsultantId.Text = dataTable.Rows[0]["ConsultantId"].ToString();
            textUserName.Text = dataTable.Rows[0]["UserName"].ToString();
            textFullName.Text = dataTable.Rows[0]["FullName"].ToString();
            textCompanyName.Text = dataTable.Rows[0]["CompanyName"].ToString();
            textAddress.Text = dataTable.Rows[0]["Address"].ToString();
            textPhoneNumber.Text = dataTable.Rows[0]["PhoneNumber"].ToString();
            textEmail.Text = dataTable.Rows[0]["Email"].ToString();
        }
        protected void ButtonEditClick(object sender, EventArgs e)
        {
            EnableTextBox();
        }
        protected void ButtonSaveClick(object sender, EventArgs e)
        {
            var eConsultant = new ConsultantEntity
                                  {
                                      ConsultantId = int.Parse((textConsultantId.Text), CultureInfo.InvariantCulture),
                                      FullName = textFullName.Text,
                                      Email = textEmail.Text,
                                      CompanyName = textCompanyName.Text,
                                      PhoneNumber = textPhoneNumber.Text,
                                      Address = textAddress.Text
                                  };
            var result = ConsultantProfileLogic.UpdateConsultants(eConsultant);

            if (result != null)
            {
                DisableTextBox();
            }
            else
            {
                var userEventEntity = new EventEntity
                {
                    UserName = CommonLogic.ConvertToString(Session["UserName"]),
                    EventType = (int)EventTypeList.UserEvent,
                    OnDate = DateTime.Now,
                    Description = "User update failed"
                };
                EventTracer.SaveEvents(userEventEntity);

            }
        }
        // Check user's permissions
        private void CheckUserPermission()
        {
            var userName = CommonLogic.ConvertToString(Session["UserName"]);
            const int functionId = (int)FunctionList.ViewConsultant;

            if (Page.Master != null)
            {
                var urlToCome = Page.Master.ResolveUrl("Presentation/AuthorizationError.aspx");

                AuthorizationChecker.IsValidAuthorization(userName, functionId, Response, urlToCome);
            }
        }
    }
}
